﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right" label-width="95px">
        <el-form-item :label="$t('coupon.id')+' :'">
          <el-input
            v-model="listQuery.id"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('coupon.id')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('coupon.useMerchantId')+' :'">
          <el-input
            v-model="listQuery.use_merchant_id"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('coupon.useMerchantId')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('coupon.status')+' :'">
          <el-select
            v-model="listQuery.status"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('coupon.status')"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-button
          style="margin-left: 20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
        <div v-show="!showSearchItem" class="showDown" @click="showSearchItem = true">
          <i class="el-icon-d-arrow-right"></i>
        </div>
        <div v-show="showSearchItem" class="showUp" @click="showSearchItem = false">
          <i class="el-icon-d-arrow-left"></i>
        </div>
      </el-form>
    </div>
    <div v-show="showSearchItem" class="filter-container">
      <el-form label-position="right" label-width="95px">
        <el-form-item :label="$t('coupon.playTime')+' :'">
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.startPlayTime"
              :picker-options="pickerOptionsB"
              type="date"
              :placeholder="$t('public.startTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.endPlayTime"
              :picker-options="pickerOptionsE"
              type="date"
              :placeholder="$t('public.endTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item :label="$t('coupon.useTime')+' :'">
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.startUseTime"
              :picker-options="pickerOptionsBU"
              type="date"
              :placeholder="$t('public.startTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
          <div class="filter-item">
            <el-date-picker
              v-model="listQuery.endUseTime"
              :picker-options="pickerOptionsEU"
              type="date"
              :placeholder="$t('public.endTime')"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      fixed="left"
      :data="list"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('coupon.id')">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('coupon.equipmentCode')">
        <template slot-scope="scope">
          <span>{{ scope.row.equipment_code }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        :label="$t('coupon.equipmentRegionName')"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.equipment_region_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('coupon.tradeName')">
        <template slot-scope="scope">
          <span>{{ scope.row.trade_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('coupon.amount')">
        <template slot-scope="scope">
          <span>{{ scope.row.coupon_amount }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('coupon.playTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.play_time }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('coupon.useMerchantId')">
        <template slot-scope="scope">
          <span>{{ scope.row.use_merchant_id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('coupon.useAmount')">
        <template slot-scope="scope">
          <span>{{ scope.row.use_amount }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('coupon.useTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.use_time }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('coupon.status')">
        <template slot-scope="scope">
          <span>{{ orderEs(scope.row.status) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "AdOrder",
  data() {
    return {
      showSearchItem: false,
      loading: false,
      tableKey: 0,
      list: null,
      total: null,
      opreaterItem: 1,
      listLoading: true,
      statusOptions: [
        {
          value: 1,
          label: "已下单"
        },
        {
          value: 2,
          label: "已完成"
        }
      ],
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined,
        use_merchant_id: undefined,
        status: undefined,
        startPlayTime: "",
        endPlayTime: "",
        startUseTime: "",
        endUseTime: ""
      },
      temp: {
        id: undefined,
        name: "",
        region_name: "",
        address: "",
        linkman: "",
        mobile: "",
        created_at: "",
        ad_income: "",
        balance: ""
      },
      dialogFormRead: false,
      dialogStatus: "",
      textMap: {
        update: "更新",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      pickerOptionsB: {
        // 限制时间
        // selectableRange: ['22:00:00 - 23:59:59','00:00:00 - 02:00:00'],
        // 限制日期
        disabledDate: this.disabledDateB
      },
      pickerOptionsE: {
        disabledDate: this.disabledDateE
      },
      pickerOptionsBU: {
        disabledDate: this.disabledDateBU
      },
      pickerOptionsEU: {
        disabledDate: this.disabledDateEU
      }
    };
  },
  computed: {
    operWidth: function() {
      return this.opreaterItem * 80 + "px";
    }
  },
  created() {
    this.getList();
  },
  methods: {
    disabledDateB(time) {
      if (this.listQuery.endPlayTime) {
        return (
          time.getTime() >
          new Date(this.listQuery.endPlayTime).getTime() +
            24 * 3600 * 1000 -
            8.64e7
        );
      }
    },
    disabledDateE(time) {
      if (this.listQuery.startPlayTime) {
        return (
          time.getTime() <
          new Date(this.listQuery.startPlayTime).getTime() - 8.64e6
        );
      }
    },
    disabledDateBU(time) {
      if (this.listQuery.endUseTime) {
        return (
          time.getTime() >
          new Date(this.listQuery.endUseTime).getTime() +
            24 * 3600 * 1000 -
            8.64e7
        );
      }
    },
    disabledDateEU(time) {
      if (this.listQuery.startUseTime) {
        return (
          time.getTime() <
          new Date(this.listQuery.startUseTime).getTime() - 8.64e6
        );
      }
    },
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.id) {
        data["id"] = this.listQuery.id;
      }
      if (this.listQuery.use_merchant_id) {
        data["use_merchant_id"] = this.listQuery.use_merchant_id;
      }
      if (this.listQuery.status || this.listQuery.status === 0) {
        data["status"] = this.listQuery.status;
      }
      if (this.listQuery.startPlayTime) {
        data["startPlayTime"] = this.listQuery.startPlayTime;
      }
      if (this.listQuery.endPlayTime) {
        const date = new Date(this.listQuery.endPlayTime);
        data["endPlayTime"] = this.timestampToTime(
          date.getTime() + (60 * 60 * 24 * 1000 - 1)
        );
      }
      if (this.listQuery.startUseTime) {
        data["startUseTime"] = this.listQuery.startUseTime;
      }
      if (this.listQuery.endUseTime) {
        const date = new Date(this.listQuery.endUseTime);
        data["endUseTime"] = this.timestampToTime(
          date.getTime() + (60 * 60 * 24 * 1000 - 1)
        );
      }
      const adMasterIndexPath = {
        // 获取订单列表
        company: "STQ",
        api: "pc_AdOrder_index",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(adMasterIndexPath), res => {
        if (res) {
          if (res !== '{"type":"ping"}') {
            const resj = JSON.parse(res);
            if (resj.code === -1) {
              this.$message.error(resj.message);
            } else {
              this.total = resj.count;
              this.list = resj.data;
            }
          }
          this.listLoading = false;
        } else {
          this.$message.error(" 获取订单列表失败 ");
          this.listLoading = false;
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        id: undefined,
        use_merchant_id: undefined,
        status: undefined,
        startPlayTime: "",
        endPlayTime: "",
        startUseTime: "",
        endUseTime: ""
      };
      this.getList();
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "read";
      this.dialogFormRead = true;
    }
  }
};
</script>
